const app = getApp();
const util = require('../../../utils/util.js');

Page({
  data: {
    userInfo: {},
    healthInfo: {},
    medicalHistory: [],
    allergies: [],
    medicines: [],
    checkupRecords: [],
    emergencyContact: {}
  },

  onLoad() {
    this.loadHealthRecord();
  },

  loadHealthRecord() {
    // 模拟数据，实际应从API获取
    const userInfo = {
      name: '张大爷',
      gender: '男',
      age: 68,
      idCard: '330102********2816',
      phone: '138****8888',
      address: '浙江省杭州市上城区XX街道XX社区'
    };

    const healthInfo = {
      bloodType: 'A型',
      height: 172,
      weight: 68,
      bmi: '23.0',
      bmiClass: 'normal'
    };

    const medicalHistory = [
      { id: 1, name: '高血压', time: '2015年确诊' },
      { id: 2, name: '糖尿病', time: '2018年确诊' }
    ];

    const allergies = ['青霉素', '海鲜', '花粉'];

    const medicines = [
      { 
        id: 1, 
        name: '降压药（XX牌）', 
        usage: '每日2次，每次1片',
        time: '08:00 / 20:00'
      },
      { 
        id: 2, 
        name: '降糖药（XX牌）', 
        usage: '每日3次，每次1片',
        time: '08:00 / 12:00 / 18:00'
      }
    ];

    const checkupRecords = [
      { 
        id: 1, 
        name: '年度全面体检', 
        time: '2023-09-15',
        result: '基本正常',
        resultClass: 'normal'
      },
      { 
        id: 2, 
        name: '血糖检测', 
        time: '2023-08-20',
        result: '偏高',
        resultClass: 'abnormal'
      },
      { 
        id: 3, 
        name: '心电图检查', 
        time: '2023-07-10',
        result: '正常',
        resultClass: 'normal'
      }
    ];

    const emergencyContact = {
      name: '李女士',
      relation: '女儿',
      phone: '139****9999'
    };

    this.setData({
      userInfo,
      healthInfo,
      medicalHistory,
      allergies,
      medicines,
      checkupRecords,
      emergencyContact
    });
  },

  onCallEmergency() {
    const phone = this.data.emergencyContact.phone.replace(/\*/g, '1');
    wx.makePhoneCall({
      phoneNumber: phone
    }).catch(() => {
      util.showToast('拨号失败');
    });
  },

  onViewAllCheckup() {
    util.showToast('查看全部体检记录');
    // 实际应跳转到体检记录列表页
  },

  onCheckupDetail(e) {
    const id = e.currentTarget.dataset.id;
    util.showToast('查看体检详情');
    // 实际应跳转到体检详情页
  },

  onEdit() {
    util.showToast('跳转编辑页面');
    // 实际应跳转到编辑页面
    // wx.navigateTo({
    //   url: '/pages/mine/health-record/edit'
    // });
  }
})

